package com.lune.job.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lune.job.pojo.Favor;
import com.lune.job.pojo.ResPageBean;
import com.lune.job.pojo.RespBean;
import com.lune.job.service.*;
import com.lune.job.vo.UserData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/favor")
@Api(tags = "职位收藏管理")

public class FavorController {

    @Autowired
    private IFavorService favorService;

    @Autowired
    private IUserDataService userDataService;

    @Autowired
    private IStudentService studentService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IPostService postService;

    @ApiOperation(value = "获取所有职位收藏")
    @GetMapping("/")
    public List<Favor> getAllDict() {
        List<Favor> list = favorService.list();
        list.forEach(item -> {
            item.setCompany(companyService.getById(item.getCompany_id()));
            item.setStudent(studentService.getById(item.getStudent_id()));
            item.setPost(postService.getById(item.getPost_id()));

        });
        return list;
    }
    @ApiOperation(value = "添加职位收藏")
    @PostMapping("/")
    public RespBean create(@RequestBody Map<String,Integer> map) {
        UserData userData = userDataService.getUserData();
        //1、判断用户是否登录
        if (userData==null){
            return RespBean.error("未登录不能操作");
        }
        Integer post_id = map.get("post_id");
        Integer company_id = map.get("company_id");

        //2、判断是否重复收藏
        Favor favorParam = new Favor();
        favorParam.setStudent_id(userData.getId());
        favorParam.setPost_id(post_id);
        favorParam.setCompany_id(company_id);
        List<Favor> favorList = favorService.queryByCon(favorParam);
        if (favorList!=null && favorList.size()>0){
            return RespBean.error("请勿重复收藏");
        }

        Favor favor = new Favor();
        favor.setPost_id(post_id);
        favor.setCompany_id(company_id);
        favor.setFavor_date(new Date());
        favor.setStudent_id(userData.getId());

        //3、收藏
        if (favorService.save(favor)) {
            return RespBean.success("添加职位收藏成功");
        } else {
            return RespBean.error("添加职位收藏失败");
        }
    }

    @ApiOperation(value = "删除单个职位收藏")
    @DeleteMapping("/{id}")
    public RespBean deleteById(@PathVariable Integer id) {
        if (favorService.removeById(id)) {
            return RespBean.success("删除职位收藏成功");
        }
        return RespBean.error("删除职位收藏失败");
    }

    @ApiOperation(value = "批量删除职位收藏")
    @DeleteMapping("/")
    public RespBean delete(Integer[] ids) {
        if (favorService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("批量删除职位收藏成功");
        } else {
            return RespBean.error("批量删除职位收藏失败");
        }
    }

    @ApiOperation(value = "修改职位收藏")
    @PutMapping("/")
    public RespBean update(@RequestBody Favor favor) {
        if (favorService.updateById(favor)) {
            return RespBean.success("修改职位收藏成功");
        } else {
            return RespBean.error("修改职位收藏失败");
        }
    }

    @ApiOperation(value = "根据id查询职位收藏")
    @GetMapping("/detail")
    public Favor detail(Integer id) {
        return favorService.getById(id);
    }

    @ApiOperation(value = "分页查询职位收藏")
    @PostMapping("/page")
    public ResPageBean queryPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                 @RequestParam(defaultValue = "10") Integer size,
                                 @RequestBody Favor favor) {
        IPage<Favor> favorIPage = favorService.queryPage(currentPage, size, favor);
        favorIPage.getRecords().forEach(item -> {
            item.setCompany(companyService.getById(item.getCompany_id()));
            item.setStudent(studentService.getById(item.getStudent_id()));
            item.setPost(postService.getById(item.getPost_id()));
        });
        return new ResPageBean(favorIPage.getTotal(), favorIPage.getPages(), favorIPage.getRecords());
    }


    @ApiOperation(value = "条件查询职位收藏")
    @PostMapping("/query")
    public List<Favor> queryDict(
            @RequestBody Favor favor) {
        List<Favor> favors = favorService.queryByCon(favor);
        return favors;
    }
}
